﻿using BillHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaxAssistantBase;

namespace DemoForBillOcr
{
    public class RestDataTool
    {
        public static string Post(out string errorMessage,  BatchInfo model, string apiurl,out List<BatchInfo> list)
        {
          
            try
            {
              
                var result = RestTool.PostA<RestResultModel1>(apiurl, model);
               
                if (result != null)
                {
                    if (result.errorCode == CloudResultStateEnum.SUCCESS)//成功
                    {
                        list = new List<BatchInfo>();
                        List<InvoiiceOcrModel> bpmlist = new List<InvoiiceOcrModel>();
                        bpmlist = (from comall in result.data select comall).ToList();
                        if (bpmlist.Count>0) {
                            foreach (InvoiiceOcrModel bpml in bpmlist) {
                                BatchInfo bi = new BatchInfo();
                                bi.batchtimes = bpml.batchtimes;
                                bi.companyName = bpml.companyName;
                                bi.companyTaxnum = bpml.companyTaxnum;
                                bi.processingnum = bpml.processingnum;
                                bi.productName = bpml.productName;
                                bi.ocrProductName = bpml.ocrProductName;
                                bi.defaultTaxRate = bpml.defaultTaxRate;
                                list.Add(bi);
                            }
                            errorMessage = result.errorMessage;
                            return errorMessage;
                        }
                        errorMessage = result.errorMessage;
                        return errorMessage;
                    }
                   
                }
                else
                {
                    list = new List<BatchInfo>();
                    errorMessage = "没有返回结果";
                    return errorMessage;
                }
                list = new List<BatchInfo>();
                errorMessage = "";
                return errorMessage;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                errorMessage = ex.Message;
                list = new List<BatchInfo>();
                return errorMessage;
            }
        }
        public static string Post2(out string errorMessage, InvoiiceOcrModel model, string apiurl, out List<InvoiiceOcrModel> list)
        {
            WsdLogger wlog = new WsdLogger("GuanLiLog");
            try
            {
                wlog.Write("Post开始");
                var result = RestTool2.PostA<RestResultModel1>(apiurl, model);
                wlog.Write("Post结束");
                if (result != null)
                {
                    if (result.errorCode == CloudResultStateEnum.SUCCESS)//成功
                    {

                        list = new List<InvoiiceOcrModel>();
                        int temp = 0;
                        try
                        {
                            foreach (InvoiiceOcrModel iom in result.data)
                            {
                                model = new InvoiiceOcrModel();
                                model.id = iom.id;
                                model.mac = iom.mac;
                                model.billingDate = iom.billingDate;
                                model.invoiceNum = iom.invoiceNum;
                                model.batchnumber = iom.batchnumber;
                                model.billtype = iom.billtype;
                                model.billtypeDesc = iom.billtypeDesc;
                                model.stateDesc = iom.stateDesc;
                                model.state = iom.state;
                                model.invoiceOcrSonListSet = iom.invoiceOcrSonListSet;
                                model.declaredate = iom.declaredate;
                                model.invoiceOcrListDetailedSet = iom.invoiceOcrListDetailedSet;
                                model.batchtime = iom.batchtime;
                                model.companyName = iom.companyName;
                                model.companyTaxnum = iom.companyTaxnum;
                                model.imgUrl = iom.imgUrl;
                                temp++;
                                if (iom.batchtime != null)
                                {
                                    if (iom.batchtime.Length > 8)
                                    {
                                        model.todaytime = Convert.ToString(iom.batchtime.Substring(0, 8));
                                    }
                                }
                                list.Add(model);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.Write("报错" + temp);
                        }


                        errorMessage = "";
                        return errorMessage;
                    }
                    wlog.Write("返回失败结果" + result.errorMessage);
                    errorMessage = result.errorMessage;
                }
                else
                {
                    errorMessage = "没有返回结果";
                    wlog.Write("没有返回结果");
                }
                list = new List<InvoiiceOcrModel>();
                return errorMessage;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                wlog.Write("返回触发异常" + ex.Message);
                errorMessage = ex.Message;
                list = new List<InvoiiceOcrModel>();
                return errorMessage;
            }
        }

        public static string PostVendors(out string errorMessage, BatchInfo model, string apiurl, out List<BatchInfo> list)
        {

            try
            {

                var result = RestTool.PostVendors<RestResultModelVendors>(apiurl, model);

                if (result != null)
                {
                    if (result.errorCode == CloudResultStateEnum.SUCCESS)//成功
                    {
                        List<BatchInfo> bpmlist = new List<BatchInfo>();
                        bpmlist = (from comall in result.data select comall).ToList();
                        if (bpmlist.Count > 0)
                        {
                            list = bpmlist;
                            errorMessage = result.errorMessage;
                            return errorMessage;
                        }
                        errorMessage = result.errorMessage;
                        list = new List<BatchInfo>();
                        return errorMessage;
                    }

                }
                else
                {
                    list = new List<BatchInfo>(); ;
                    errorMessage = "没有返回结果";
                    return errorMessage;
                }
                list = new List<BatchInfo>(); ;
                errorMessage = "";
                return errorMessage;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                errorMessage = ex.Message;
                list = new List<BatchInfo>(); 
                return errorMessage;
            }
        }
    }

    internal class T
    {
        public IList<T> data { get; set; }
    }
}
